package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.projectmoudle_two.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by eyesee on 2017/6/18.
 */

public class SortFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private View view;
    /**
     * 倒序
     */
    private Button mDaoxu;
    private ListView mSortlistview;
    private List<Integer> mList;
    private SortAdapter mSortAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = View.inflate(mContext, R.layout.fragment_sort, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        prepareData();
        initView(mView);
        return mView;
    }

    private void prepareData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 500; i++) {
            mList.add(i);
        }
    }

    private void initView(View view) {
        mSortlistview = (ListView) view.findViewById(R.id.sortlistview);
        mDaoxu = (Button) view.findViewById(R.id.daoxu);

        mSortAdapter = new SortAdapter(mList);
        mSortlistview.setAdapter(mSortAdapter);

        mDaoxu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.daoxu:
                List<Integer> daoxu = daoxu(mList);
                SortAdapter adapter = new SortAdapter(daoxu);
                mSortlistview.setAdapter(adapter);
                break;
        }
    }

    public List<Integer> daoxu(List<Integer> list) {
        Collections.reverse(list);
        return list;
    }

    private class SortAdapter extends BaseAdapter{
        private List<Integer> mList;

        SortAdapter(List<Integer> list) {
            mList = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mList==null?0:mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView==null){
                convertView = View.inflate(mContext,R.layout.list_item,null);
            }
            ((TextView) convertView.findViewById(R.id.tv)).setText(mList.get(position)+"");
            return convertView;
        }
    }


}
